from flask import Blueprint,request,make_response,jsonify,redirect
import json, random, time, requests
from utils.myredis import r
from models.models import *

serve_blue = Blueprint("serveblue",__name__)

@serve_blue.route("/qneed",methods=["POST"])
# 抢需求
def qneed():
    data = json.loads(request.data)
    jdr = data["userid"]
    code = data["code"]
    # hash取模
    number = hash(int(code)) % 3
    if number == 0:
        res = Need.query.filter_by(code=code).first()
        if res:
            res.status = 3,
            res.jdid = jdr
            db.session.add(res)
            db.session.commit()
        return jsonify({"code": 200, "msg": "已抢"})
    else:
        needs = "Need" + str(number)
        needs = globals().get(f'{needs}')
        res = needs.query.filter_by(code=code).first()
        if res:
            res.status = 3,
            res.jdid = jdr
            db.session.add(res)
            db.session.commit()
        return jsonify({"code":200,"msg":"已抢"})

# 获取当前用户的需求
@serve_blue.route("/userneed")
def userneed():
    userid = int(request.args.get("userid"))
    ret = db.session.execute("select *  from (select * from need where status=3 and jdid=%d union all select * from need1 where status=3 and jdid=%d union all select * from need2 where status=3 and jdid=%d) as t;"%(userid,userid,userid))
    lst = []
    for i in ret:
        lst.append({
            'code': str(i.code),
            'title': i.title,
            "money": i.money,
            "described": i.described,
            "closeid": i.closeid,
            "userid": i.userid
        })
    return jsonify({"code":200,"msg":"获取成功","data":lst})

# 用户提交需求
@serve_blue.route("/tjneed")
def tjneed():
    code = request.args.get("code")
    number = hash(int(code)) % 3
    tname = "Need"
    if number != 0:
         tname = tname + str(number)
    # 转为表名
    tablename = globals().get(f'{tname}')
    r = tablename.query.filter_by(code=code).first()
    if r:
        r.status = 4
        db.session.add(r)
        db.session.commit()
        return jsonify({"code": 200, "msg": "提交成功"})

# 获取服务商已提交的需求
@serve_blue.route("/funeed")
def funeed():
    userid = int(request.args.get("userid"))
    ret = db.session.execute("select *  from (select * from need where status=4 and userid=%d union all select * from need1 where status=4 and userid=%d union all select * from need2 where status=4 and userid=%d) as t;"%(userid,userid,userid))
    lst = []
    for i in ret:
        lst.append({
            'code': str(i.code),
            'title': i.title,
            "money": i.money,
            "described": i.described,
            "closeid": i.closeid,
            "userid": i.userid,
            "jdid":i.jdid
        })
    return jsonify({"code": 200, "msg": "获取成功", "data": lst})

# 雇主提交支付佣金
@serve_blue.route("/zfneed")
def zfneed():
    jdid = request.args.get("jdid")
    userid = request.args.get("userid")
    money = request.args.get("money")
    code = request.args.get("code")
    print(jdid,userid,money)
    if not all([jdid,userid,money,code]):
        return jsonify({"code": 400, "msg": "字段不能为空"})
    user = User.query.filter_by(id=jdid).first()
    if user:
        # 更新服务商的金额
        user.money = float(user.money) + float(money)
        db.session.add(user)
        db.session.commit()
        users = User.query.filter_by(id=userid).first()
        if users:
            # 更新雇主的锁定金额
            users.locks = float(users.locks) - float(money)
            db.session.add(users)
            db.session.commit()
            # 修改需求状态
            # print(code)
            number = hash(int(code)) % 3
            tname = "Need"
            if number != 0:
                tname = tname + str(number)
            # 转为表名
            tablename = globals().get(f'{tname}')
            r = tablename.query.filter_by(code=code).first()
            if r:
                r.status = 5
                db.session.add(r)
                db.session.commit()
                return jsonify({"code":200,"msg":"支付成功"})









